package com.quasar.hpatient.module.comm_web;

import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.quasar.hpatient.R;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.widget.web.CusWebTitle;

/**
 * description: H5网页
 * created by kalu on 2017/5/25 13:46
 */
public class WebActivity extends BaseActivity<WebPresenter> {

    public static final String WEB_URL = "WEB_URL";

    private TextView webClose;
    private CusWebTitle webTitle;
    private RelativeLayout webParent;

    @Override
    public int initView() {
        return R.layout.activity_comm_web;
    }

    @Override
    public void initDataNet() {
        webClose = (TextView) findViewById(R.id.web_html_close);
        webTitle = (CusWebTitle) findViewById(R.id.web_html_title);
        webParent = (RelativeLayout) findViewById(R.id.web_parent);

        getPresenter().initWebClient(getApplicationContext(), webParent, webTitle, webClose);

        String htmlUrl = getIntent().getStringExtra(WEB_URL);
        getPresenter().loadWebHtml(htmlUrl);

        findViewById(R.id.web_html_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != getPresenter() && !getPresenter().canGoBack()) {
                    onBackPressed();
                }
            }
        });

        findViewById(R.id.web_html_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    /*******************************************************/

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && getPresenter().canGoBack()) {
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (null == getPresenter()) return;
        getPresenter().pauseResource();
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (null == getPresenter()) return;
        getPresenter().resumeResource();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (null == getPresenter()) return;
        getPresenter().destoryResource(webParent);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // android.os.Process.killProcess(android.os.Process.myPid());
    }
}